package api

import (
	"context"
	"doctor_srv/global"
	"doctor_srv/model"
	"doctor_srv/model/dao"
	pb "doctor_srv/proto"
	"github.com/google/uuid"
)

func (d *DoctorServer) DoctorList(ctx context.Context, req *pb.DoctorListRequest) (resp *pb.DoctorListResponse, err error) {

	doctor := dao.GetDoctor(req.StartTime, req.EndTime, int(req.DId))

	var result []*pb.Doctors

	for _, v := range doctor {
		result = append(result, &pb.Doctors{
			Id:         int32(v.ID),
			Name:       v.Name,
			RoomId:     int32(v.RoomID),
			HospitalId: int32(v.HospitalID),
			Image:      v.Image,
			Intro:      v.Intro,
		})
	}
	return &pb.DoctorListResponse{Doctors: result}, nil
}

func (d *DoctorServer) Create(ctx context.Context, req *pb.CreateRequest) (resp *pb.CreateResponse, err error) {

	Consultation := model.Consultation{
		Code:    uuid.NewString(),
		Name:    req.Name,
		History: req.His,
		Age:     int(req.Age),
		Sex:     int(req.Sex),
		IsNot:   0,
		Intro:   "",
		Image:   "",
		Times:   0,
	}

	global.DB.Create(&Consultation)
	return &pb.CreateResponse{Success: true}, nil
}
func (d *DoctorServer) LIstanbul(ctx context.Context, req *pb.LIstanbulRequest) (resp *pb.LIstanbulResponse, err error) {

	var consultation []model.Consultation

	global.DB.Order("times DESC").Where("created_at BETWEEN ? AND ?", req.Start, req.End).Find(&consultation)

	var result []*pb.Consultation

	for _, v := range consultation {
		result = append(result, &pb.Consultation{
			Id:      int32(v.ID),
			Name:    v.Name,
			Age:     int32(v.Age),
			Sex:     int32(v.Sex),
			History: v.History,
			Intro:   v.Intro,
		})
	}

	return &pb.LIstanbulResponse{List: result}, nil
}
